/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","ajaxServ","msgServ","sysmanMain", "bizProto","btnServ","flowServ","panelUtil","flowHistoric"],
		function($,datagridServ,ajaxServ,msgServ, sysmanMain, bizProto,btnServ,flowServ,panelUtil,flowHistoric) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,_defaultConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=700,window_btn,
				
				//流程执行信息grid
				_historicGrid,
				//业务表单对象
				$form,
				_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:null,
						moduleCode:null,
						editWindow:null,
						btnSelector:'div.flow-edit-btn',
						initQueryParams:null,
						initElement:null,
						historicUrl:null,
						historicField:null,
						buttonExtends:null
					}
				
				
			var public_method = {				
				
				init:function(_options){
					
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if (window.console) {
						console.info( _moduleConfig.fullBizcode + ' modules gets ready and been loaded.');
					}
					
					
					_defaultConfig = $.extend({},_defaultConfig,_options)			
					_bizProto = new bizProto(_defaultConfig);			
					_bizProto.gridHeight -= 15
					
					_moduleConfig.bizProto=_bizProto
					_moduleConfig.defaultConfig = _defaultConfig
					
					
					
					if(_defaultConfig.editWindow){
						$edit_window = $('#'+_defaultConfig.editWindow)
						_moduleConfig.edit_window = _defaultConfig.editWindow
					}
					
					_buttonConfig={										    
						 "保存":private_method.submit,
						 "取消":private_method.cancel,
						 "发送":private_method.flowSend,
						 "签收":private_method.flowSign						 
					}
					if(_defaultConfig.buttonExtends){
						var _extends = _defaultConfig.buttonExtends()
						if(_extends)
						$.extend(_buttonConfig,_extends)
					}
					
					_moduleConfig.buttonConfig = _buttonConfig
					//保存模块初始化信息
					sysmanMain.setModuConf(_moduleConfig.fullBizcode,_moduleConfig)
					private_method.init();					
				}
				
			}

			var private_method = {
				
				
				//初始化元素
				init : function() {
									
					var _tab = sysmanMain.getCurrentTab();
					$grid = $(_tab).find("table.dg-list");					
					
					// 编辑窗口初始化									
					$form = $($edit_window).find('form.main-form')
					$form.form('clear')
					// 编辑窗口初始化					
					var editTd = $($edit_window).find(_defaultConfig.btnSelector)
					
					var _fullBizcode = sysmanMain.getCurrentBizcode();
					if(editTd && editTd.length>0){
						var _param = {
							bizcode:_fullBizcode,
							rootPath:_bizProto.rootPath,
							container:editTd,
							callback:private_method.bindBtnEvent}
						btnServ.generateEditBtn(_param);
					}
					
					//页面初始化参数
					var _initParam = {form:$form}
					//加载流程执行信息
					var selectedRecord=$grid.datagrid('getSelected');
					if(selectedRecord){
						$.ajax({
							url:_bizProto.rootPath +'/executeMonitor/query/getOneByIdentify.action',
							data:{identify:selectedRecord.identify},
							type:'post',
							success:function(data){
								$form.form('load',data)
							}
						})
						
						_historicGrid= $($edit_window).find('table.historics')						
						
						if(_historicGrid && _historicGrid.length>0){
							//初始化节点信息查询配置						
							var _historicParam ={
								historicId:_historicGrid,
								record:selectedRecord
							}							
								
							if(_defaultConfig.historicField){
								_historicParam.historicField =_defaultConfig.historicField						
							}
							
							flowHistoric.initExternalGrid(_historicParam)
						}
						//判断是否调用组件初始化方法						
						_initParam.record=selectedRecord						
					}
					//判断是否执行页面初始化
					if(_defaultConfig.initElement){_defaultConfig.initElement(_initParam)}
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				
				// 提交form
				submit : function() {
					if (window.console) {
						console.info('bugEdit submit on air ...');
					}
					var _param = {form:$form,grid:$grid,url:_bizProto.flowSaveUrl}
					if(_moduleConfig.flowid){
						_param.flowid = _moduleConfig.flowid						
					}
					if(_moduleConfig.fullBizcode){						
						_param.fullBizcode = _moduleConfig.fullBizcode
					}
					datagridServ.save(_param)					
				},
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				//签收
				flowSign:function(){
					var isOpend = private_method.getOperatePanelState();
					if(!isOpend){
						private_method.changeOperatePanelState(1);
						return
					}
					var _data = private_method.getFlowMsg();
					if(_data && _data.signInfo){
						$.extend(_data,{refreshGrid:_historicGrid,flowSignUrl:_bizProto.flowSignUrl})
						flowServ.sign(_data);
					}else{						
						msgServ.Msgslide('请填写签收意见')
					}
					
				},
				//发送
				flowSend:function(){
					var _data = private_method.getFlowMsg();
					if(_data && _data.signInfo){
						$.extend(_data,{refreshGrid:$grid,flowTaskCompleteUrl:_bizProto.flowTaskCompleteUrl,closeWindow:$edit_window})
						flowServ.taskComplete(_data);
					}else{
						var isOpend = private_method.getOperatePanelState();
						if(!isOpend){
							private_method.changeOperatePanelState(1);							
						}
						msgServ.Msgslide('请填写签收意见')
					}
				},
				getFlowMsg:function(){
					var _data = {},taskElement,signElement,instElement
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.edit_window){
						$edit_window = $('#'+_moduleConfig.edit_window)
						instElement =$($edit_window).find('form.main-form #flowInstId')
						taskElement =$($edit_window).find('form.main-form #flowTaskId')
						signElement =$($edit_window).find('fieldset.form-operator input.signInfo')
						_data = {flowInstId:$(instElement).val(),flowTaskId:$(taskElement).val(),signInfo:$(signElement).val()}
					}
					
					return _data;
				},
				
				/**
				 * get the flow operate panel state
				 */
				getOperatePanelState:function(){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.edit_window){
						var flowOperatePanel = $('#'+_moduleConfig.edit_window +' div.flow-operate-panel')
						if(flowOperatePanel && flowOperatePanel.length>0){
							return panelUtil.getPanelState({target:flowOperatePanel})						
						}
					}
				},
				
				/**
				 * @param state 1,open panel;0,close panel
				 */
				changeOperatePanelState:function(state){
					_moduleConfig = sysmanMain.getCurrentModuConfig();
					if(_moduleConfig.edit_window){
						var flowOperatePanel = $('#'+_moduleConfig.edit_window +' div.flow-operate-panel')
						if(flowOperatePanel && flowOperatePanel.length>0){
							if(state){
								$(flowOperatePanel).panel('open')
								private_method.getTransitionInfo();
							}else{
								$(flowOperatePanel).panel('close')
							}
						}
					}
				},
				/**
				 * 获取流程下一节点信息
				 */
				getTransitionInfo:function(){
					var flowInfo = private_method.getFlowMsg()
					if(flowInfo && flowInfo.flowInstId){
						_moduleConfig = sysmanMain.getCurrentModuConfig();
						if(_moduleConfig.edit_window){
							var $nodeSelect = $('#'+_moduleConfig.edit_window +' div.flow-operate-panel form.flow-operate-form #flownode')
							if($nodeSelect && $nodeSelect.length>0){
								$($nodeSelect).combobox({
									url:_moduleConfig.bizProto.rootPath + '/workflow/flowOperator/findTaskOutgoing.action?procInstId='+flowInfo.flowInstId,
									valueField:'id',
									textField:'name'
								})
							}
						}
					}
					
				}
				

			};
			
			return public_method;

		});